<!-- 编辑弹窗 -->
<template>
    <ele-modal
      width="30%"
      :movable="false"
      :visible="visible"
      :close-on-click-modal="true"
      custom-class="ele-dialog-form"
      :title="isUpdate ? '修改费用管理信息' : '添加费用管理信息'"
      @update:visible="updateVisible" 
    >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px" >
        <el-card  >        
          <el-row :gutter="24">
            <el-col :md="24" :sm="24">
                <el-form-item label="费用类型:" prop="costTypeId">
                  <el-select v-model="form.costTypeId" clearable placeholder="请选择费用类型" style="width:100%">
                    <el-option v-for="(item, index) in costTypeDatas" :key="index" :label="item.name" :value="item.id" />
                  </el-select>
                </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24">
              <el-form-item label="金额:" prop="amount">
                <el-input v-model="form.amount" placeholder="请输入金额"   >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24" >
              <el-form-item label="发生日期:"  prop="peddingDate">
                <el-date-picker v-model="form.peddingDate" type="date" placeholder="请输入发生日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd" style="width:100%"/>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24" >
              <el-form-item label="支出账户:" prop="bankAccountId">
                <el-select v-model="form.bankAccountId"  placeholder="请选择支出账户" style="width:100%">
                    <el-option v-for="(item, index) in bankAccountDatas" :key="index" :label="item.name" :value="item.id" />
                  </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24" >
              <el-form-item label="经办人:" >
                <el-select v-model="form.operatorId"  placeholder="请选择经办人" style="width:100%" @change="changeOperator">
                    <el-option v-for="(item, index) in operatorDatas" :key="index" :label="item.name" :value="item.id" />
                  </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24" >
              <el-form-item label="备注:">
                <el-input v-model="form.remark" placeholder="请输入备注"  :rows="4" type="textarea" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save"  v-permission="'Financial_Cost_Save'">
        保存
      </el-button>
    </div>
    </ele-modal>
  </template>
  <script>

  //初始化对像
  import { getUser } from '@/utils/user-util';
  import { isIntegerOrDecimal2} from '@/utils/validate';
  import { GetAllAccount } from '@/api/usermanager/accountmanager';
  import { GetAllBackAccount } from '@/api/basicdatamanager/bankaccount';
  import { QueryAllChildDictViewByCode } from '@/api/systemmanager/dictmanager';
  import { AddCostmanager,UpdateCostmanager,GetCostById } from '@/api/finance/cost';
  export default {
    name: 'CostEdit',
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
    },
    data() {
      return {
        // 表单数据
        form: {},
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
        defaultDate:new Date(),
        costTypeDatas:[],
        bankAccountDatas:[],
        operatorDatas:[],
        // 表单验证规则
        rules: {
          costTypeId: [{required: true,message: '请输入费用类型',trigger: 'blur'}],
          amount: [{required: true,message: '请输入金额',trigger: 'blur'},{validator:isIntegerOrDecimal2,trigger:'blur'}],
          peddingDate: [{required: true,message: '请输入发生日期',trigger: 'blur'}],
          bankAccountId: [{required: true,message: '请输入支出账户',trigger: 'blur'}],
        },
      };
    },
    created(){
      this.initData();
    },
    methods: {

      /* 初始化数量 */
      initData(){

        GetAllAccount().then((data)=>{
          this.operatorDatas = [];
          this.operatorDatas = data;
        });

        GetAllBackAccount().then((data)=>{
          this.bankAccountDatas = [];
          this.bankAccountDatas = data;
        });

        QueryAllChildDictViewByCode(this.Constant.C_GLFY).then((data)=>{
          this.costTypeDatas = [];
          data.map((d)=>{
            if(d.isSys ==null || !d.isSys){
              this.costTypeDatas.push(d);
            }
          })
        });

      },

      /* 保存编辑 */
      save() {
        //校验必填信息
        this.$refs['form'].validate((valid) => {
          if (!valid) {return false;}

          this.loading = true;
          const saveOrUpdate = this.isUpdate ? UpdateCostmanager : AddCostmanager;
          saveOrUpdate(this.form)
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.updateVisible(false);
              //回调父组件方法
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        });
      },

      setData(id){
        GetCostById(id)
          .then((res) => {
            this.form = res; 
        });
      },

      changeOperator(val){
       let operator =  this.operatorDatas.find((d)=>{
          return d.id == val;
        });
        if(operator){
          this.form.operatorName = operator.name;
        }
      },

      /* 更新visible */
      updateVisible(value) {
        //更新父组件中的visible的值
        this.$emit('update:visible', value);
        //更新文件列表对像
      },
      
  },
    
    /* 监听visible属性变化 */
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data && this.data.id) {
            //赋值this.form
            this.form = {};
            this.setData(this.data.id);
            this.isUpdate = this.data.id !=null && this.data.id !=undefined;
          } else {
            this.form={};
            this.isUpdate = false;

            let currentUser = getUser();

            this.$set(this.form,'peddingDate',this.defaultDate);
            this.$set(this.form,'operatorId',currentUser.accountId);
            this.$set(this.form,'operatorName',currentUser.accountName)
          }
        } else {
            this.$refs['form'].clearValidate();
            this.form = {};
        }
      }
    }
  };
  </script>